#include "FiveWin.ch"

#define LB_ADDSTRING         ( WM_USER +  1 )
#define LB_SETCURSEL         ( WM_USER +  7 )
#define LB_GETCURSEL         ( WM_USER +  9 )
#define LB_DIR               ( WM_USER + 14 )
#define LB_ERR                           -1

#define LBS_NOTIFY                         1   // 0x0001L
#define LBS_DISABLENOSCROLL             4096   // 0x1000L

//----------------------------------------------------------------------------//

CLASS TListBox FROM TControl

   DATA   aItems
   DATA   bSetGet

   METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd ) CONSTRUCTOR
   METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId ) CONSTRUCTOR
   METHOD Add( cItem, nAt )
   METHOD cToChar() BLOCK { | Self | Super:cToChar( "LISTBOX" ) }
   METHOD Init( hDlg )
   METHOD SetFocus( lOnOff )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd ) CLASS TListBox

   DEFAULT aItems := {}, nWidth := 40, nHeight := 40

   ::cCaption = ""
   ::nTop     = nRow * 14
   ::nLeft    = nCol * 8
   ::nBottom  = ::nTop  + nHeight - 1
   ::nRight   = ::nLeft + nWidth - 1
   ::aItems   = aItems
   ::bSetGet  = bSetGet
   ::oWnd     = oWnd
   ::nStyle   = nOR( LBS_NOTIFY, WS_TABSTOP, LBS_DISABLENOSCROLL,;
                     WS_CHILD, WS_VISIBLE, WS_BORDER, WS_VSCROLL )
   ::nId      = ::GetNewId()

   if oWnd:ClassName() != "TDIALOG"
      ::Create( "LISTBOX" )
   else
      oWnd:DefControl( Self )
   endif

return

//----------------------------------------------------------------------------//

METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId ) CLASS TListBox

   DEFAULT aItems := {}

   ::nId     = nId
   ::hWnd    = 0
   ::aItems  = aItems
   ::bSetGet = bSetGet
   ::oWnd    = oWnd
   ::nHelpId = nHelpId

   oWnd:DefControl( Self )

return

//----------------------------------------------------------------------------//

METHOD Add( cItem, nAt ) CLASS TListBox

   DEFAULT nAt := 0

   if nAt == 0
      AAdd( ::aItems, cItem )
   else
      ASize( ::aItems, Len( ::aItems ) + 1 )
      AIns( ::aItems, nAt )
      ::aItems[ nAt ] = cItem
   endif

   ::SendMsg( LB_ADDSTRING, nAt, cItem )

return

//----------------------------------------------------------------------------//

METHOD Init( hDlg ) CLASS TListBox

   local nAt
   local cStart := Eval( ::bSetGet )

   Super:Init( hDlg )
   AEval( ::aItems, { | cItem | ::Add( cItem ) } )

   nAt = AScan( ::aItems, { | cItem | cItem == cStart } )
   if nAt != 0
      ::SendMsg( LB_SETCURSEL, nAt - 1 )
   endif

return

//----------------------------------------------------------------------------//

METHOD SetFocus( lOnOff ) CLASS TListBox

   local nAt

   if ! lOnOff
      nAt = ::SendMsg( LB_GETCURSEL )
      if nAt != -1
         Eval( ::bSetGet, ::aItems[ nAt + 1 ] )
      endif
   endif

return

//----------------------------------------------------------------------------//
